package cn.com.ddapp.ddm.util;

import android.content.Context;
import android.content.res.Configuration;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.Banner;
import cn.com.ddapp.ddm.widget.jazzyLoopViewPager.BannerViewPagerAdapter;
import cn.com.ddapp.ddm.widget.jazzyLoopViewPager.JazzyLoopViewPager;
import cn.com.ddapp.ddm.widget.jazzyLoopViewPager.JazzyViewPager;

/**
 * Created by Administrator on 2016/6/14 0014.
 */
public class HomeBannerUtil {

	public static View getloopviewpager(Context context, ArrayList<Banner> banners,String type) {
		RelativeLayout bannerRel;
		final TextView textView;
//		final LinearLayout ll_focus_indicator_container;
		JazzyLoopViewPager loopViewPager;
		bannerRel = (RelativeLayout) LayoutInflater.from(context).inflate(
				R.layout.banner_home_viewpager, null);

		textView = (TextView) bannerRel.findViewById(R.id.tv_intro);
//		ll_focus_indicator_container = (LinearLayout) bannerRel
//				.findViewById(R.id.dot_layout);

		loopViewPager = (JazzyLoopViewPager) bannerRel
				.findViewById(R.id.viewPager);
		// loopViewPager.setPageTransformer(true, new DepthPageTransformer());//
		// 设置动画
		loopViewPager.setTransitionEffect(JazzyViewPager.TransitionEffect.Tablet);// 设置动画
		// mJazzy.setOutlineEnabled(true);
		// loopViewPager.setRun(false);//设置不播放
		RelativeLayout.LayoutParams layoutparams = (RelativeLayout.LayoutParams) loopViewPager
				.getLayoutParams();
		DisplayMetrics metric = new DisplayMetrics();

		WindowManager mWm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		mWm.getDefaultDisplay().getMetrics(metric);

		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			layoutparams.width = metric.widthPixels * 298 / 720;
			layoutparams.height = metric.widthPixels /2;
//			layoutparams.height = (int) (metric.widthPixels * 298 / 720);
		} else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			layoutparams.width = metric.widthPixels * 550 / 720;
			layoutparams.height = metric.widthPixels * 550 / 720;
		}

		loopViewPager.setLayoutParams(layoutparams);

//		initImgList(ll_focus_indicator_container, banners,context);
		BannerViewPagerAdapter viewPagerAdapter = new BannerViewPagerAdapter(
				null, context, banners,type);
		loopViewPager.setAdapter(viewPagerAdapter);

		final int count = banners.size();
		textView.setText("1/"+count);
		loopViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
//				Log.d("loopViewPager", arg0 + "");
//				indicatorChange(ll_focus_indicator_container, arg0);
				textView.setText((arg0+1) + "/" + count);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {


			}

			@Override
			public void onPageScrollStateChanged(int arg0) {


			}
		});
		// 如果图片在最后一张跳转时有闪烁，可以设置为TURE
		loopViewPager.setBoundaryCaching(true);

		return bannerRel;
	}
    public static View getloopviewpager2(Context context, ArrayList<Banner> banners,String type) {
        RelativeLayout bannerRel;
        final TextView textView;
        final LinearLayout ll_focus_indicator_container;
        JazzyLoopViewPager loopViewPager;
        bannerRel = (RelativeLayout) LayoutInflater.from(context).inflate(
                R.layout.banner_home_viewpager, null);

        textView = (TextView) bannerRel.findViewById(R.id.tv_intro);
        textView.setVisibility(View.GONE);
        ll_focus_indicator_container = (LinearLayout) bannerRel
                .findViewById(R.id.dot_layout);
        ll_focus_indicator_container.setVisibility(View.VISIBLE);

        loopViewPager = (JazzyLoopViewPager) bannerRel
                .findViewById(R.id.viewPager);
        // loopViewPager.setPageTransformer(true, new DepthPageTransformer());//
        // 设置动画
        loopViewPager.setTransitionEffect(JazzyViewPager.TransitionEffect.Tablet);// 设置动画
        // mJazzy.setOutlineEnabled(true);
        // loopViewPager.setRun(false);//设置不播放
        RelativeLayout.LayoutParams layoutparams = (RelativeLayout.LayoutParams) loopViewPager
                .getLayoutParams();
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager mWm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mWm.getDefaultDisplay().getMetrics(metric);
        layoutparams.width = metric.widthPixels;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            layoutparams.height = (int) (metric.widthPixels * 298 / 720);
        } else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            layoutparams.height = metric.widthPixels * 500 / 720;
        }

        loopViewPager.setLayoutParams(layoutparams);

        initImgList(ll_focus_indicator_container, banners, context);
        BannerViewPagerAdapter viewPagerAdapter = new BannerViewPagerAdapter(
                null, context, banners,type);
        loopViewPager.setAdapter(viewPagerAdapter);

        final int count = banners.size();
        textView.setText("1/" + count);
        loopViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
//				Log.d("loopViewPager", arg0 + "");
                indicatorChange(ll_focus_indicator_container, arg0);
//				textView.setText((arg0+1) + "/" + count);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {


            }

            @Override
            public void onPageScrollStateChanged(int arg0) {


            }
        });
        // 如果图片在最后一张跳转时有闪烁，可以设置为TURE
        loopViewPager.setBoundaryCaching(true);

        return bannerRel;
    }
	private static void initImgList(LinearLayout ll_focus_indicator_container,
								ArrayList<Banner> banners, Context context) {
		ll_focus_indicator_container.removeAllViews();
		for (int i = 0; i < banners.size(); i++) {
			ImageView localImageView = new ImageView(context);
			localImageView.setId(i);
			ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
			localImageView.setScaleType(localScaleType);
		LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
					DensityUtils.dp2px(context, 15), DensityUtils.dp2px(context, 15));  //改变指标大小
			localLayoutParams.setMargins(10, 4, 10, 4);
			localImageView.setLayoutParams(localLayoutParams);
			localImageView.setPadding(4, 4, 4, 4);

			localImageView.setImageResource(R.mipmap.ic_launcher);
			localImageView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});
			ll_focus_indicator_container.addView(localImageView);
		}

		indicatorChange(ll_focus_indicator_container, 0);
	}

	/**
	 * 修改指标focus
	 *
	 * @return
	 */
	private static void indicatorChange(LinearLayout layout, int position) {
	for (int i = 0; i < layout.getChildCount(); i++) {
			ImageView imageView = (ImageView) layout.getChildAt(i);

			if (i == position) {
				imageView.setImageResource(R.mipmap.w_vip);
		} else {
				imageView.setImageResource(R.mipmap.dot_normal);
		}
		}

	}
}
